<template>
  <div class="chat-layout">
    <!-- 侧边栏 - Kimi风格 -->
    <div class="sidebar" :class="{ collapsed: chatStore.isSidebarCollapsed }">
      <div class="sidebar-header">
        <!-- Logo区域 -->
        <div class="logo-section">
          <div class="logo" v-if="!chatStore.isSidebarCollapsed">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
              <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z" fill="var(--kimi-primary)"/>
            </svg>
            <span class="logo-text">AI助手</span>
          </div>
          <button 
            class="toggle-btn" 
            @click="chatStore.toggleSidebar"
            :title="chatStore.isSidebarCollapsed ? '展开侧边栏' : '折叠侧边栏'"
          >
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
              <path v-if="chatStore.isSidebarCollapsed" d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z" fill="currentColor"/>
              <path v-else d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z" fill="currentColor"/>
            </svg>
          </button>
        </div>
        
        <!-- 新建对话按钮 -->
        <button 
          v-if="!chatStore.isSidebarCollapsed"
          :disabled="chatStore.isSending"
          class="new-chat-btn" 
          @click="handleNewChat"
        >
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
            <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" fill="currentColor"/>
          </svg>
          <span>新建对话</span>
        </button>
      </div>

      <!-- 历史对话列表 -->
      <div v-if="!chatStore.isSidebarCollapsed" class="chat-history">
        <div class="history-header">
          <span>历史对话</span>
          <span class="count">{{ chatStore.chatSessions?.length || 0 }}</span>
        </div>
        
        <div class="chat-list" v-if="chatStore.chatSessions?.length > 0">
          <div 
            v-for="chat in chatStore.chatSessions" 
            :key="chat.id"
            class="chat-item"
            :class="{ active: chatStore.currentChatId === chat.session_id, new: chat.isNew }"
            @click="selectChat(chat.session_id)"
          >
            <div class="chat-icon">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                <path d="M20 2H4c-1.1 0-1.99.9-1.99 2L2 22l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zM6 9h12v2H6V9zm8 5H6v-2h8v2zm4-6H6V6h12v2z" fill="currentColor"/>
              </svg>
            </div>
            
            <div class="chat-content">
              <div class="chat-title">{{ chat.title }}</div>
              <div class="chat-time">{{ formatTime(chat.created_at) }}</div>
            </div>
            
            <!-- 操作菜单 -->
            <div class="chat-menu" @click.stop>
              <button 
                class="menu-btn" 
                @click="showMenu = showMenu === chat.id ? null : chat.id"
              >
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                  <path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z" fill="currentColor"/>
                </svg>
              </button>
              
              <div v-if="showMenu === chat.id" class="menu-dropdown">
                <button class="menu-item delete-btn" @click="deleteChat(chat.session_id)">
                  <svg width="14" height="14" viewBox="0 0 24 24" fill="none">
                    <path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z" fill="currentColor"/>
                  </svg>
                  删除对话
                </button>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 空状态 -->
        <div v-if="chatStore.chatSessions?.length === 0" class="empty-state">
          <svg width="48" height="48" viewBox="0 0 24 24" fill="none">
            <path d="M20 2H4c-1.1 0-1.99.9-1.99 2L2 22l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zM6 9h12v2H6V9zm8 5H6v-2h8v2zm4-6H6V6h12v2z" fill="var(--kimi-border)"/>
          </svg>
          <p>暂无对话记录</p>
          <span>开始新的对话吧</span>
        </div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <router-view />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useChatStore } from '@/stores/chatStore'

const router = useRouter()
const chatStore = useChatStore();

const showMenu = ref(null)

// 关闭菜单
const closeMenu = () => {
  showMenu.value = null
}

// 点击其他地方关闭菜单
const handleClickOutside = (event) => {
  if (!event.target.closest('.chat-menu')) {
    closeMenu()
  }
}

// 新建对话
const handleNewChat = () => {
  if (chatStore.isSending) return
  chatStore.resetChat()
  // 刷新推荐问题
  chatStore.setRecommendedQuestions()
  router.push(`/chat`)
  closeMenu()
}

// 选择对话
const selectChat = (chatId) => {
  if (chatStore.isSending) return
  console.log(chatId)
  router.push(`/chat/${chatId}`)
  chatStore.setCurrentChat(chatId)
  closeMenu()
}

// 删除对话
const deleteChat = (chatId) => {
  if (chatStore.isSending) return
  if (chatId === chatStore.currentChatId) {
    router.push('/chat')
  }
  chatStore.deleteChat(chatId)
  closeMenu()
}

// 格式化时间
const formatTime = (date) => {
  const now = new Date()
  const chatDate = new Date(date)
  const diff = now.getTime() - chatDate.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (days === 0) {
    return chatDate.toLocaleTimeString('zh-CN', { 
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit', 
      minute: '2-digit' 
    })
  } else if (days === 1) {
    return '昨天'
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return chatDate.toLocaleDateString('zh-CN', {
      month: 'short',
      day: 'numeric'
    })
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
  chatStore.setChatSessions() // 初始化时获取对话列表
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.chat-layout {
  display: flex;
  height: 100vh;
  background: var(--kimi-bg);
}

/* 侧边栏样式 */
.sidebar {
  width: 280px;
  background: var(--kimi-sidebar-bg);
  border-right: 1px solid var(--kimi-border);
  display: flex;
  flex-direction: column;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  z-index: 10;
}

.sidebar.collapsed {
  width: 64px;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--kimi-border);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.logo-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 16px;
  color: var(--kimi-text-primary);
}

.logo-text {
  background: linear-gradient(135deg, var(--kimi-primary) 0%, #3b82f6 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.toggle-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--kimi-text-secondary);
  transition: all 0.2s;
}

.toggle-btn:hover {
  background: var(--kimi-hover);
  color: var(--kimi-text-primary);
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--kimi-primary);
  color: white;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
  width: 100%;
}

.new-chat-btn:hover {
  background: var(--kimi-primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--kimi-shadow);
}

/* 历史对话区域 */
.chat-history {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 8px;
  overflow-y: auto;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--kimi-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.count {
  background: var(--kimi-border);
  color: var(--kimi-text-secondary);
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 11px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.chat-item:hover {
  background: var(--kimi-hover);
}

.chat-item.active {
  background: white;
  box-shadow: var(--kimi-shadow);
}

.chat-item.new {
  background: rgba(16, 163, 127, 0.05);
}

.chat-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--kimi-border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--kimi-text-secondary);
  flex-shrink: 0;
}

.chat-content {
  flex: 1;
  min-width: 0;
}

.chat-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--kimi-text-primary);
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-time {
  font-size: 12px;
  color: var(--kimi-text-secondary);
}

.chat-menu {
  position: relative;
  flex-shrink: 0;
}

.menu-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--kimi-text-secondary);
  opacity: 0;
  transition: all 0.2s;
}

.chat-item:hover .menu-btn {
  opacity: 1;
}

.menu-btn:hover {
  background: var(--kimi-hover);
  color: var(--kimi-text-primary);
}

.menu-dropdown {
  position: absolute;
  right: 0;
  top: 100%;
  background: white;
  border: 1px solid var(--kimi-border);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 20;
  min-width: 120px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--kimi-text-primary);
  transition: background 0.2s;
}

.menu-item:hover {
  background: var(--kimi-hover);
}

.delete-btn {
  color: #ef4444;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
  color: var(--kimi-text-secondary);
  height: 100%;
}

.empty-state p {
  margin: 12px 0 4px 0;
  font-weight: 500;
  color: var(--kimi-text-primary);
}

.empty-state span {
  font-size: 13px;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--kimi-bg);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
  }
  
  .sidebar:not(.collapsed) {
    transform: translateX(0);
  }
  
  .sidebar.collapsed {
    width: 280px;
    transform: translateX(-100%);
  }
}
</style>